﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;
using KPIS.GERP.GIMS.APP.APP_UI.Report;
using KPIS.GERP.GIMS.APP.APP_CODE;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.BLL;
using System.IO;

namespace KPIS.GERP.GIMS.APP.APP_UI.Form
{
    public partial class ViewReport : System.Web.UI.Page
    {
        SysReportBLL bll = new SysReportBLL();
        SysReportDataTableBLL bllDtb = new SysReportDataTableBLL();
        SysControlBLL bllControl = new SysControlBLL();

        public void Page_Load(object sender, EventArgs e)
        {
        }

        protected void CrystalReportViewer1_Init(object sender, EventArgs e)
        {
            int seq = Convert.ToInt32(Request.QueryString["rpt_seq"]);
            SysReportInfo info = bll.GetReport(seq);
            LoadReport(info.Name_en);
        }

        protected void LoadReport(string ReportName)
        {
            switch (ReportName)
            {
                case "RPT_PR_APPROVE":
                    LoadPurchaseRequestApprove();
                    break;
                case "RPT_PO_APPROVE":
                    LoadPurchaseOrderApprove();
                    break;
                case "RPT_DN_APPROVE":
                    LoadDeliveryNoteApprove();
                    break;
                case "RPT_DN_CONTRACT_APPROVE":
                    LoadDeliveryNoteContractApprove();
                    break;
                case "RPT_PO_CONTRACT":
                    LoadPurchaseOrderContract();
                    break;
            }
        }
        protected void LoadPurchaseOrderContract()
        {
            //R_ACM_BALANCE_Sheet reportBalanceSheet = new R_ACM_BALANCE_Sheet();
            //reportBalanceSheet.SetDataSource(d_acm_general_ledger.REPORT_BALANCE_Sheet());
            //reportBalanceSheet.SetParameterValue("pStartDate", Session["sStartDate"].ToString());
            //reportBalanceSheet.SetParameterValue("pEndDate", Session["sEndDate"].ToString());
            //reportBalanceSheet.SetParameterValue("vStartDate", Session["dStartDate"]);
            //reportBalanceSheet.SetParameterValue("vEndDate", Session["dEndDate"]);
            //reportBalanceSheet.SetParameterValue("CreateBy", main.strUserID);
            //CrystalReportViewer1.PageToTreeRatio = 8.5;
            //CrystalReportViewer1.ReportSource = reportBalanceSheet;
            //CrystalReportViewer1.DataBind();
        }

        protected void LoadPurchaseRequestApprove()
        {
            int seq = Convert.ToInt32(Request.QueryString["seq"]);
            string strWinDescr = bllDtb.GetPurchaseRequestApproveItemWinner(seq);
            string strBuyDescr = bllDtb.GetPurchaseRequestApproveItemList(seq);
            string[] date_value = bllDtb.GetPurchaseRequestDate(seq);
            string strDate = (date_value[0].Length > 0
                ? " ภายในวันที่ " + bllControl.FormatBuddhistDate(date_value[0])
                : " ภายใน " + date_value[1] + " " + date_value[2] + "นับจาก" + date_value[3]);
            INV_PR_AGREE report = new INV_PR_AGREE();
            report.SetDataSource(bllDtb.GetPurchaseRequestApproveMain(seq, strBuyDescr, strWinDescr));
            report.OpenSubreport("INV_PR_AGREE_SUB1.rpt");
            report.Subreports[0].SetDataSource(bllDtb.GetPurchaseRequestApproveItem(seq));
            report.OpenSubreport("INV_PR_AGREE_SUB2.rpt");
            report.Subreports[1].SetDataSource(bllDtb.GetPurchaseRequestApproveBidderItem(seq));
            report.OpenSubreport("INV_PR_AGREE_SUB3.rpt");
            report.Subreports[2].SetDataSource(bllDtb.GetPurchaseRequestApproveCommittee(seq));
            //Viewer1.ReportSource = report;
            ExportReport(report);
        }

        protected void LoadPurchaseOrderApprove()
        {
            FormatMoney fMoney = new FormatMoney();
            int seq = Convert.ToInt32(Request.QueryString["seq"]);
            string strSumAmount = fMoney.MoneyTextThai(bllDtb.GetPurchaseOrderNetAmount(seq), "บาท", "สตางค์");
            string strPenalty = bllDtb.GetPurchaseOrderPenalty(seq);
            string strDate = bllDtb.GetPurchaseOrderDate(seq);
            strDate = bllControl.FormatBuddhistDate(strDate);
            
            INV_PO report = new INV_PO();
            report.SetDataSource(bllDtb.GetPurchaseOrderApproveMain(seq));
            report.SetParameterValue(0, strSumAmount);
            report.SetParameterValue(1, strPenalty);
            report.SetParameterValue(2, strDate);
            //Viewer1.ReportSource = report;
            ExportReport(report);
        }

        protected void LoadDeliveryNoteApprove()
        {
            int seq = Convert.ToInt32(Request.QueryString["seq"]);
            string[] strDescr = bllDtb.GetDeliveryNoteReceiveItem(seq);
            string strPoDescr = strDescr[0];
            string strDnDescr = strDescr[1];
            INV_DN report = new INV_DN();
            report.SetDataSource(bllDtb.GetDeliveryNoteApproveMain(seq, strPoDescr, strDnDescr));
            report.OpenSubreport("INV_DN_COMMITTEE.rpt");
            report.Subreports[0].SetDataSource(bllDtb.GetDeliveryNoteCommittee(seq));
            //Viewer1.ReportSource = report;
            ExportReport(report);
        }

        protected void LoadDeliveryNoteContractApprove()
        {
            int seq = Convert.ToInt32(Request.QueryString["seq"]);
            string[] strDescr = bllDtb.GetDeliveryNoteReceiveItem(seq);
            string strPoDescr = strDescr[0];
            string strDnDescr = strDescr[1];
            INV_DN_CONTRACT report = new INV_DN_CONTRACT();
            report.SetDataSource(bllDtb.GetDeliveryNoteApproveMain(seq, strPoDescr, strDnDescr));
            report.OpenSubreport("INV_DN_COMMITTEE.rpt");
            report.Subreports[0].SetDataSource(bllDtb.GetDeliveryNoteCommittee(seq));
            //Viewer1.ReportSource = report;
            ExportReport(report);
        }

        protected void ExportReport(ReportClass report)
        {
            string content = Request.QueryString["content"];
            MemoryStream oStream = new MemoryStream();
            Response.Clear();
            Response.Buffer = true;

            switch (content)
            {
                case "pdf":
                    oStream = (MemoryStream)report.ExportToStream(ExportFormatType.PortableDocFormat);
                    Response.ContentType = "application/pdf";
                    break;
                case "word":
                    oStream = (MemoryStream)report.ExportToStream(ExportFormatType.WordForWindows);
                    Response.ContentType = "application/msword"; 
                    break;
                case "excel":
                    oStream = (MemoryStream)report.ExportToStream(ExportFormatType.Excel);
                    Response.ContentType = "application/vnd.ms-excel"; 
                    break;
            }
               
            Response.BinaryWrite(oStream.ToArray());
            Response.End();
        }
    }
}
